import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:management/utils/custom_router.dart';
import 'package:management/views/visitor/query/visitor_record_query.dart';
import 'package:management/views/visitor/visitor_card.dart';
import 'package:management/views/visitor/checkIn/id-card-register-page.dart';

///访客登记
class VisitorRegistration extends StatefulWidget {
  @override
  _VisitorRegistrationState createState() => _VisitorRegistrationState();
}

class _VisitorRegistrationState extends State<VisitorRegistration> {
  //获取系统时间初始化指针位置
  // int mdeg = DateTime.now().second * 6; //秒针
  // int fdeg = DateTime.now().minute * 6; //分针
  // int sdeg = DateTime.now().hour * 30 +
  //     (DateTime.now().minute * 6 / 72 * 6).toInt(); //时针
  // Timer timePeriodic;

  @override
  void initState() {
    super.initState();
    SystemChrome.setEnabledSystemUIOverlays([]);
    // setDeg();
  }

  //设置指针位置
  // void setDeg() async {
  //   timePeriodic = Timer.periodic(Duration(seconds: 1), (timer) {
  //     if (mounted) {
  //       //每过12分钟 时针走一格
  //       if (fdeg % 72 == 0) {
  //         //并且在秒针指向12的时候
  //         if (mdeg == 360) {
  //           sdeg += 6;
  //         }
  //       }
  //       if (mdeg == 360) {
  //         //每过60秒 分针走一格
  //         mdeg = 0;
  //
  //         fdeg += 6;
  //       }
  //
  //       if (fdeg == 360) {
  //         fdeg = 0;
  //       }
  //
  //       if (sdeg == 360) {
  //         sdeg = 0;
  //       }
  //
  //       setState(() {
  //         //秒针每秒走一格
  //         mdeg += 6;
  //       });
  //     }
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            "访客服务",
            style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
          ),
          centerTitle: true,
          backgroundColor: Colors.blue,
        ),
        body: Stack(
          children: [
            Container(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              child: Image.asset(
                'assets/images/blue_sky.jpg',
                fit: BoxFit.cover,
              ),
            ),
            CardPositionView(
              assetPath: 'assets/images/idCard.png',
              text: '访客登记',
              fontSize: 25,
              top: 20,
              left: 20,
              onTap: () {
                // MyLog.i("身份证登记");
                // Navigator.of(context).pushNamed('/idCardRegistration');
                Navigator.push(
                    context, FadeRoute(IDCardRegisterPage(), seconds: 2));
              },
            ),
            CardPositionView(
              icon: Icon(
                Icons.my_library_books_sharp,
                size: 50,
                color: Colors.white,
              ),
              text: '访客记录查询',
              fontSize: 25,
              textMargin: EdgeInsets.only(top: 10),
              onTap: () {
                // MyLog.i("访客记录查询");
                // Navigator.of(context).pushNamed("/VisitorRecordQuery");
                Navigator.push(
                  context,
                  FadeRoute(VisitorRecordQuery(), seconds: 2),
                );
              },
              top: 20,
              left: 440,
            ),

            // Positioned(
            //   child: MyClock(
            //     mdeg: mdeg,
            //     fdeg: fdeg,
            //     sdeg: sdeg,
            //   ),
            //   right: 20,
            //   top: 20,
            // ),
            // CardPositionView(
            //   text: "测试",
            //   textColor: Colors.greenAccent,
            //   textMargin: EdgeInsets.only(top: 10),
            //   top: 240,
            //   left: 20,
            //   onTap: () =>  Navigator.push(context, ScaleRoute(IDCardRegisterPage())),
            // )

            // CardPositionView(
            //   name: 'assets/images/other_documents.png',
            //   text: '其他证件登记',
            //   left: 440,
            //   top: 20,
            //   onTap: () {
            //     MyLog.i('其他证件登记');
            //   },
            // ),
            // CardPositionView(
            //   name: 'assets/images/visitor_id.png',
            //   text: "访客证签离",
            //   left: 20,
            //   top: 240,
            //   onTap: () {
            //     MyLog.i("访客证签离");
            //   },
            // ),
            // CardPositionView(
            //   name: "assets/images/no_card_sign_out.png",
            //   text: "无证签离",
            //   left: 440,
            //   top: 240,
            //   onTap: () {
            //     MyLog.i("无证签离");
            //   },
            // ),
          ],
        ));
  }

  @override
  void dispose() {
    // timePeriodic.cancel();
    // timePeriodic = null;
    super.dispose();
  }
}

//时钟
class MyClock extends StatelessWidget {
  ///秒针扫过的角度
  final int mdeg;

  ///分针扫过的角度
  final int fdeg;

  ///时针扫过的角度
  final int sdeg;

  const MyClock(
      {Key? key, required this.mdeg, required this.fdeg, required this.sdeg})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      child: CustomPaint(
        painter: ClockPaint(this.mdeg, this.fdeg, this.sdeg,
            secondHandColor: Colors.greenAccent),
        size: Size(300, 300),
      ),
    );
  }
}

class ClockPaint extends CustomPainter {
  ClockPaint(
    this.mdeg,
    this.fdeg,
    this.sdeg, {
    this.cx = 150,
    this.cy = 150,
    this.r = 100,
    this.secondHandColor = Colors.green,
  });

  final int mdeg; //秒针扫过的角度
  final int fdeg; //分针扫过的角度
  final int sdeg; //时针扫过的角度

  final double cx; //圆心横坐标
  final double cy; //圆心纵坐标
  final double r; //半径

  //时针长度
  int hourHandLength = 40;

  //时针宽度
  double hourHandWidth = 8;

  //时针颜色
  Color hourHandColor = Colors.red;

  //分针长度
  int minuteHandLength = 80;

  //分针宽度
  double minuteHandWidth = 8;

  //分针颜色
  Color minuteHandColor = Colors.white;

  //秒针长度
  int secondHandLength = 80;

  //秒针宽度
  double secondHandWidth = 2;

  //秒针颜色
  Color secondHandColor = Colors.green;

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = Colors.black87
      ..isAntiAlias = true
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.fill;

    //画黑色圆盘
    canvas.drawCircle(Offset(cx, cy), r, paint);

    //画时针 长度40
    paint.strokeWidth = hourHandWidth / 2;
    paint.color = hourHandColor;
    int sdeg = this.sdeg;
    double srad = sdeg / 180.0 * pi;
    canvas.drawLine(Offset(cx, cy),
        Offset(cx + sin(srad) * 25, cy - cos(srad) * 25), paint);
    paint.strokeWidth = hourHandWidth;
    canvas.drawLine(
        Offset(cx + sin(srad) * 25, cy - cos(srad) * 25),
        Offset((cx + sin(srad) * 25) + sin(srad) * 30,
            (cy - cos(srad) * 25) - cos(srad) * 30),
        paint);

    //画分针 长度80
    paint.strokeWidth = minuteHandWidth / 2;
    paint.color = minuteHandColor;
    int fdeg = this.fdeg;
    double frad = fdeg / 180.0 * pi;
    canvas.drawLine(Offset(cx, cy),
        Offset(cx + sin(frad) * 25, cy - cos(frad) * 25), paint);
    paint.strokeWidth = minuteHandWidth;
    canvas.drawLine(
        Offset(cx + sin(frad) * 25, cy - cos(frad) * 25),
        Offset(
          (cx + sin(frad) * 25) + sin(frad) * (minuteHandLength - 25),
          (cy - cos(frad) * 25) - cos(frad) * (minuteHandLength - 25),
        ),
        paint);

    //画秒针   走一秒扫过6度
    paint.strokeWidth = secondHandWidth;
    paint.color = secondHandColor;
    int mdeg = this.mdeg; //角度
    double mrad = mdeg / 180.0 * pi; //角度转弧度
    canvas.drawLine(
        Offset(cx, cy),
        Offset(cx + sin(mrad) * secondHandLength,
            cy - cos(mrad) * secondHandLength),
        paint);

    //画红色圆心
    paint.strokeWidth = 1;
    paint.style = PaintingStyle.fill;
    paint.color = Colors.red;
    canvas.drawCircle(Offset(cx, cy), 6, paint);

    //画红色圆心外面的白圈
    paint.strokeWidth = 3;
    paint.style = PaintingStyle.stroke;
    paint.color = Colors.white;
    canvas.drawCircle(Offset(cx, cy), 7, paint);

    // paint.strokeWidth = 2;
    // paint.style = PaintingStyle.stroke;
    // paint.color = Colors.green;
    // canvas.drawCircle(Offset(100,100), 9, paint);

    //画时间段
    paint.strokeWidth = 5;
    paint.color = Colors.white;
    canvas.drawPoints(
        PointMode.points,
        [
          Offset(cx, cy - r + paint.strokeWidth / 2),
          Offset(cx + r - paint.strokeWidth / 2, cy),
          Offset(cx, cy + r - paint.strokeWidth / 2),
          Offset(cx - r + paint.strokeWidth / 2, cy)
        ],
        paint);
    paint.color = Colors.grey;
    canvas.drawPoints(
        PointMode.points,
        [
          Offset(cx + sin(30 / 180.0 * pi) * r,
              cy - cos(30 / 180.0 * pi) * r + paint.strokeWidth / 2),
          Offset(cx + sin(60 / 180.0 * pi) * r,
              cy - cos(60 / 180.0 * pi) * r + paint.strokeWidth / 2),
          Offset(cx + sin(120 / 180.0 * pi) * r - paint.strokeWidth / 2,
              cy - cos(120 / 180.0 * pi) * r),
          Offset(cx + sin(150 / 180.0 * pi) * r - paint.strokeWidth / 2,
              cy - cos(150 / 180.0 * pi) * r),
          Offset(cx + sin(210 / 180.0 * pi) * r,
              cy - cos(210 / 180.0 * pi) * r - paint.strokeWidth / 2),
          Offset(cx + sin(240 / 180.0 * pi) * r,
              cy - cos(240 / 180.0 * pi) * r - paint.strokeWidth / 2),
          Offset(cx + sin(300 / 180.0 * pi) * r + paint.strokeWidth / 2,
              cy - cos(300 / 180.0 * pi) * r),
          Offset(cx + sin(330 / 180.0 * pi) * r + paint.strokeWidth / 2,
              cy - cos(330 / 180.0 * pi) * r),
        ],
        paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
